package pyf.demo;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;

/**
 * @author pengyifeng
 * @version 5.1
 * @ClassName TestThreadDemo03.java
 * @Description 多线程方式03 Callable
 * @createTime 2021年07月26日 18:25:00
 */
public class TestThreadDemo03 implements Callable {

    private String url;
    private String fileName;

    TestThreadDemo03(String url, String fileName){
        this.url=url;
        this.fileName=fileName;
    }
    @Override
    public Boolean call() throws Exception {
         new DownFile().down(url,fileName);
        return true;
    }

    class DownFile {

        public  void down(String url, String fileName) throws IOException {
            URL url1 = new URL(url);
            FileUtils.copyURLToFile(url1, new File("d:/img/" + fileName));
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestThreadDemo03 t1=new TestThreadDemo03("https://blog.gitee.com/wp-content/uploads/2020/02/logo_blog_light.png", "下载1");
        TestThreadDemo03 t2=new TestThreadDemo03("https://blog.gitee.com/wp-content/uploads/2020/02/logo_blog_light.png", "下载2");
        TestThreadDemo03 t3=new TestThreadDemo03("https://blog.gitee.com/wp-content/uploads/2020/02/logo_blog_light.png", "下载3");
        //创建执行服务
        ExecutorService service= Executors.newFixedThreadPool(3);
        //提交执行
        Future f1 = service.submit(t1);
        Future f2 = service.submit(t2);
        Future f3 = service.submit(t3);

        //获取结果
        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());

        //关闭服务
        service.shutdown();


    }
}
